<?php

class adminSection extends  divForm
{
	public function __construct()
	{
	    global $sections;
	    global $page;

	    //Delete
	    $actionRemove = getActionResult('tableSection', 'remove' );

	    if ($actionRemove)
	    {
	        if ($ok = $sections->autoRemove(array('sectionId'=>$actionRemove))  )
	        {
	            $page->addJsCode("alert('Registro removido com sucesso.');");
	        }
	        else
	        {
	            $page->addJsCode("alert('Falha na remocao do registro!');");
	        }

	    }

	    $actionUpdate = getActionResult('tableSection');

	    if ($actionUpdate)
	    {
	        $section = $sections->getSection($actionUpdate);
	    }

	    $columns =  $sections->getColumns() ;

	    if ( is_array($columns) )
	    {
	        foreach ( $columns as $line => $col)
	        {
	            $pos = stripos(trim($col->type), 'boolean');

	            $type = null;

	            if ($pos !== false)
	            {
	                $type = 'boolean';
	            }

	            if ($type != 'boolean')
	            {
	                $fields[] = new Text($col->name, $section[$col->name], 'text', ucfirst($col->name) );
	            }
	            else
	            {
	                $arrayYesNo['0'] = 'No';
	                $arrayYesNo['1'] = 'Yes';
	                $value = $section[$col->name];
	                $fields[] = new SSelect($col->name, $value , $arrayYesNo, 'text', ucfirst($col->name) );
	            }
	        }
	    }

	    $save      = new submit('save',  'Inserir', 'columnTitle');
	    $save      -> onClick('insertSection');

	    $fields[]  = $save;

	    $list       = $sections->listAll();
	    $columns    = array('sectionId', 'name', 'component', 'args', 'isActive', 'level');
	    $temp       = new Grid('tableSection', $columns, $list, null, null ,null, null, 'columnTitle', 'text');
	    $fields[]   = $temp;
	    $form       = new Easyform('form01', $fields,'index.php', 'post',null,'form');

	    parent::divForm('divSection', $form, 778, null , "Secoes", 'text', 'columnTitle');
	}
}

function insertSection()
{
    global $page;
    global $sections;
    //var_dump($_REQUEST);
    $ok  = $sections->insertSection($_REQUEST);

    if (!$ok)
    {
        $page->addJsCode("alert('Falha na operacao!')");
    }
    else
    {
        $page->addJsCode("alert('Secao inserida com sucesso!')");
    }
}
?>